var app = getApp();
var dateTimePicker = require('../../utils/dateTimePicker.js');
var date = new Date();
var currentYear = date.getFullYear()-1;//如果不减去1 就是默认从今年向后1年 共计2年，如果减去1 那就是去年、今年、明年共计三年
var nextYear = currentYear + 2;

Page({

  data: {
    showgif:true,
    hindex: 0,
    rulerhou:'',
    date: '2018-10-01',
    time: '12:00',
    leixing:'',
    dateTimeArray2: null,
    dateTime2: null,
    dateTimeArray1: null,
    dateTime1: null,
    startYear: '',
    endYear: '',
    typename:'提交变更申请',
    status:'',
    statusindex:"",
    choose:'',
    info:{},
    startDate:'请选择上班时间',
    endDate:'请选择下班时间',
    pageInfo: '',
    index: 0,
    eindex: 0,
    index2: 0,
    array: [],
    earray: [],
    array2: ['上班', '下班'],
    date: '',
    liyou:'',
    workz:'',
    date2:'',
    hou:'',
    options:'',
    riqi:'',
    if1:true,
    if2:false,
    id:'',
  },

  onLoad: function (options) {
    var that = this;
    if (!wx.getStorageSync('openid')) {
      app.error("请登录后再操作");
      return false;
    }
    var id = options.id;
    this.setData({
      id:id,
      startYear: currentYear,
      endYear: nextYear,
    });
    this.getinfo(id);

      // 获取完整的年月日 时分秒，以及默认显示的数组
      var obj2 = dateTimePicker.dateTimePicker(currentYear, nextYear);
      var obj1 = dateTimePicker.dateTimePicker(currentYear, nextYear);
      // 精确到分的处理，将数组的秒去掉

      this.setData({
        dateTime2: obj2.dateTime,
        dateTimeArray2: obj2.dateTimeArray,
        dateTimeArray1: obj1.dateTimeArray,
        dateTime1: obj1.dateTime,
      });
 
  },
  //页面显示信息
  getinfo:function(id){
    var that = this;
    wx.request({
      url: app.apiUrl + '/api/index/jswgetxbinfo',
      data: {
        openid:wx.getStorageSync('openid'),
        id:id
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
        that.setData({showgif:false});
        if(res.data.code == 0){
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          });
    
        }else{
            this.setData({info:res.data.data});  
      
        }
   
      }
    })
  },

//角色变更事件
bindPickerChange_hx(e) {
  console.log('picker发送选择改变，索引为', e.detail.value);
  　var rulerhou = this.data.hArray[e.detail.value].hou //根据索引去查对应的id
  　var hname = this.data.hArray[e.detail.value].name //根据索引去查对应的id
    console.log(hname)
  this.setData({   //给变量赋值
      hindex: e.detail.value,
      rulerhou:rulerhou,
      hname:hname
 })
},
//时间控件start
changeDate(e){
       this.setData({ date:e.detail.value});
},
changeTime(e){
       this.setData({ time: e.detail.value });
},
changeDateTime(e){
       this.setData({ dateTime: e.detail.value });
},
changeDateTime2(e) {
  var that = this; 
  this.setData({
    dateTime2: e.detail.value,
    endDate: that.data.dateTimeArray2[0][that.data.dateTime2[0]]+"-"+that.data.dateTimeArray2[1][that.data.dateTime2[1]]+"-"+that.data.dateTimeArray2[2][that.data.dateTime2[2]]+" "+that.data.dateTimeArray2[3][that.data.dateTime2[3]]+":"+that.data.dateTimeArray2[4][that.data.dateTime2[4]]
  });


},
changeDateTime1(e) {
  
  var that = this;
  this.setData({
    dateTime1: e.detail.value, 
    startDate: that.data.dateTimeArray1[0][that.data.dateTime1[0]]+"-"+that.data.dateTimeArray1[1][that.data.dateTime1[1]]+"-"+that.data.dateTimeArray1[2][that.data.dateTime1[2]]+" "+that.data.dateTimeArray1[3][that.data.dateTime1[3]]+":"+that.data.dateTimeArray1[4][that.data.dateTime1[4]]
  });
},
changeDateTimeColumn(e){
       var arr = this.data.dateTime, dateArr = this.data.dateTimeArray;   
       arr[e.detail.column] = e.detail.value;
       dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
       this.setData({
         dateTimeArray: dateArr,
         dateTime: arr
       });
}, 
changeDateTimeColumn1(e) {
       var arr = this.data.dateTime1, dateArr = this.data.dateTimeArray1;
       arr[e.detail.column] = e.detail.value;
       dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
       this.setData({ 
         dateTimeArray1: dateArr,
         dateTime1: arr
       });
},
changeDateTimeColumn2(e) {
  var arr = this.data.dateTime2, dateArr = this.data.dateTimeArray2;
  arr[e.detail.column] = e.detail.value;
  dateArr[2] = dateTimePicker.getMonthDay(dateArr[0][arr[0]], dateArr[1][arr[1]]);
  this.setData({ 
    dateTimeArray2: dateArr,
    dateTime2: arr
  });
},

//时间控件end
  formSubmit: function (event) {
    var that = this;
    var edate = that.data.endDate;

    if(edate == '请选择下班时间'){
      app.error('请选择下班时间');
      return false;
    }
    var liyou = event.detail.value.liyou;

    that.setData({showgif:true});
 
    wx.request({
      url: app.apiUrl + '/api/index/jswupdatebuka',
      data: {
        xiaban:edate,
        id:this.data.id,
        liyou:liyou
      },

      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
        that.setData({showgif:false});
        if(res.data.code == 0){
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          });
    
        }else{
          wx.showModal({
            title: '温馨提示',
            content: res.data.msg,
            showCancel: false
          });
          setTimeout(function () {
            wx.redirectTo({
              url: '/page/my/index',
            })
          }, 2000)
       
        }
   
      }
    })

  },

  bindKeyInput(e){
      console.log(e.detail.value)
      this.setData({ liyou: e.detail.value});
  },

  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },

  ebindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      edate: e.detail.value
    })
  },


  bindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  ebindPickerChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      eindex: e.detail.value
    })
  },

})